package no.ntnu.fp.handlers;

import java.io.IOException;

import no.ntnu.fp.client.exceptions.NetworkException;
import no.ntnu.fp.handler.Handler;
import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.impl.LoginFailedMessage;
import no.ntnu.fp.model.LoggedInModel;
import no.ntnu.fp.model.LoggedInModel.LoginError;
import no.ntnu.fp.net.co.Connection;

public class LoginFailedHandler implements Handler {

	private final LoggedInModel loggedInModel;
	private final Connection connection;

	public LoginFailedHandler(Connection connection, LoggedInModel loggedInModel) {
		this.connection = connection;
		this.loggedInModel = loggedInModel;
	}

	@Override
	public void handleMessage(Message message) {
		loggedInModel.setLoggedIn(false, LoginError.INVALID_USER_CREDENTIALS);
		try {
			System.err.println("Huh");
			connection.close();
			System.err.println("Did it close?");
		} catch (IOException e) {
			System.err.println("Huh");
			throw new NetworkException("Unable to close connection at login failure", e);
		}
	}

}
